//
//  THAddTextView.m
//  duolaCard
//
//  Created by 希达 on 2018/5/8.
//  Copyright © 2018年 zyy. All rights reserved.
//

#import "THAddTextView.h"
#import "THColorView.h"

#import <TZImagePickerController/TZImagePickerController.h>
#import <YYKit/YYKit.h>


@interface THAddTextView ()
@property (weak, nonatomic) UIImageView *bgImgView;
@property (weak, nonatomic) UIButton *cancelBtn;
@property (weak, nonatomic) UIButton *doneBtn;
@property (weak, nonatomic) UITextView *textView;
@property (weak, nonatomic) THColorView *colorView;
@end

@implementation THAddTextView
{
    BOOL _isShowKeyBoard;
}
- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.6];
        [self configView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(th_keyboardDidChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    return self;
}

- (void)configView {
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.bounds];
    bgImgView.userInteractionEnabled = YES;
    [self addSubview:bgImgView];
    self.bgImgView = bgImgView;

    CGFloat top = TZCommonTools.tz_isIPhoneX ? 44 : 0;
    CGFloat bottom = TZCommonTools.tz_isIPhoneX ? 34 : 0;
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, top, 100, 44)];
    [cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;

    UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 100, top, 100, 44)];
    [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:doneBtn];
    self.doneBtn = doneBtn;

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(doneBtn.frame), self.frame.size.width - 40, self.frame.size.height - CGRectGetMaxY(doneBtn.frame) - 44 - bottom)];
    textView.backgroundColor = [UIColor clearColor];
    textView.font = [UIFont systemFontOfSize:36];
    [self addSubview:textView];
    self.textView = textView;

    THColorView *colorView = [[THColorView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(textView.frame), self.frame.size.width, 44)];
    [self addSubview:colorView];
    self.colorView = colorView;

    @weakify(self);
    [colorView setTh_collectionViewDidSelectItemBlock:^(THColorModel *model) {
        NSLog(@"--%@--", model.colorName);
        @strongify(self);
        if ([model.colorName containsString:@"#"]) {
            self.textView.textColor = [UIColor colorWithHexString:model.colorName];
        } else {
            self.textView.font = [UIFont fontWithName:model.colorName size:36];
        }
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [textView becomeFirstResponder];
    });
}

- (void)setText:(NSString *)text {
    _text =text;
    self.textView.text = text;
}

- (void)setColorArray:(NSMutableArray *)colorArray {
    _colorArray = colorArray;
    self.colorView.isShowFont = YES;
    self.colorView.colorArray = colorArray;
    for (THColorModel *model in colorArray) {
        if (model.isSelected) {
            if ([model.colorName containsString:@"#"]) {
                self.textView.textColor = [UIColor colorWithHexString:model.colorName];
            } else {
                self.textView.font = [UIFont fontWithName:model.colorName size:36];
            }
        }
    }
}

- (void)setBgImage:(UIImage *)bgImage {
    _bgImage = bgImage;
    self.bgImgView.image = bgImage;
}

- (void)cancelBtnClick:(UIButton *)sender {
    if (self.th_addTextCancelBtnClickBlock) {
        self.th_addTextCancelBtnClickBlock();
    }
}

- (void)doneBtnClick:(UIButton *)sender {
    if (self.th_addTextDoneBtnClickBlock) {
        self.th_addTextDoneBtnClickBlock(self.textView);
    }
}

- (void)th_keyboardDidChangeFrameNotification:(NSNotification *)notification {

    CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if (endFrame.origin.y == [UIScreen mainScreen].bounds.size.height) {
        CGFloat bottom = TZCommonTools.tz_isIPhoneX ? 34 : 0;
        self.textView.frame = CGRectMake(20, CGRectGetMaxY(self.doneBtn.frame), self.frame.size.width - 40, self.frame.size.height - CGRectGetMaxY(self.doneBtn.frame) - 44 - bottom);
        self.colorView.top = CGRectGetMaxY(self.textView.frame);
    } else {
        self.textView.frame = CGRectMake(20, CGRectGetMaxY(self.cancelBtn.frame), self.frame.size.width - 40, self.frame.size.height - CGRectGetMaxY(self.doneBtn.frame) - 44 - endFrame.size.height);
        self.colorView.top = CGRectGetMaxY(self.textView.frame);
    }
}



@end
